package com.mall.common.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.mall.common.listener.EntityListener;
import com.mall.common.util.JaxbDateAdapter;
import org.hibernate.search.annotations.DocumentId;

/**
 * 实体类 - 基类
 */
@MappedSuperclass
@Inheritance(strategy=InheritanceType.JOINED)
@EntityListeners({EntityListener.class})
public class BaseEntity implements Serializable {
	private static final long serialVersionUID = -6718838800112233445L;

	private Long id;// ID
	private Date createDate;// 创建日期
	private Date modifyDate;// 修改日期

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@DocumentId
	public Long getId() {
		return id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	@Column(updatable = false, name="createDate")
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	@XmlJavaTypeAdapter(JaxbDateAdapter.class)
	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}
	
	public boolean equals(Object obj) {
		if (obj == null){
			return false;
		}
		if (this == obj){
			return true;
		}
		if (!BaseEntity.class.isAssignableFrom(obj.getClass())) {
			return false;
		} else {
			BaseEntity baseEntity = (BaseEntity)obj;
			return getId() == null ? false : getId().equals(baseEntity.getId());
		}
	}

	public int hashCode() {
		int i = 17;
		i += getId() != null ? getId().hashCode() * 31 : 0;
		return i;
	}

}